package com.chaoyi.controller;

import com.alibaba.fastjson.JSONObject;
import com.chaoyi.doman.koreanvideo.VideoCollectionOutput;
import com.chaoyi.model.KoreanVideo;
import com.chaoyi.model.VideoCollection;
import com.chaoyi.service.ChannelSwitchService;
import com.chaoyi.service.VideoCollectionService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: 卜加超
 * @Date: 2018/12/26 09:58
 * @Description:
 */
@Controller
@RequestMapping("/videoCollection")
public class VideoCollectionController {
    @Resource
    private VideoCollectionService videoCollectionService;


    @RequestMapping("/list")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ModelAndView mod = new ModelAndView("/videoCollection/videoCollectionList");
        return mod;
    }

    @RequestMapping("videoCollectionList")
    @ResponseBody
    public JSONObject videoList(int offset, int limit, VideoCollectionOutput videoCollectionOutput) {
        JSONObject json = new JSONObject();

        int count =videoCollectionService.findCount(videoCollectionOutput);

        List<VideoCollectionOutput> list = new ArrayList<VideoCollectionOutput>();
        if(count>0){
            list = videoCollectionService.findList(offset, limit,videoCollectionOutput);
        }
        json.put("rows", list);
        System.out.println(list.toString());
        json.put("total", count);
        return json;
    }
    @RequestMapping("/add_plus")
    public ModelAndView addVideoCollection(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ModelAndView mod = new ModelAndView("/videoCollection/videoCollection_add");
        return mod;
    }
    @ResponseBody
    @RequestMapping("addVideoCollectionSave")
    public JSONObject addVideoSave(VideoCollection entity) {
        JSONObject json = new JSONObject();
        int number = videoCollectionService.addVideoCollectionSave(entity);
        if (number == 1) {
            json.put("missage", "success");
        } else {
            json.put("missage", "error");
        }
        return json;
    }
    @RequestMapping("/alertVideoCollection")
    public ModelAndView alertVideoCollection(HttpServletRequest request, HttpServletResponse response,int id)
            throws ServletException, IOException {

        ModelAndView mod = new ModelAndView("/videoCollection/videoCollection_update");
        VideoCollectionOutput videoCollectionOutput = videoCollectionService.selectVideoOne(id);
        mod.addObject("obj",videoCollectionOutput);
        return mod;
    }
    /**
     * 保存修改
     */
    @ResponseBody
    @RequestMapping("edit")
    public JSONObject alertVideoCollectionSave(HttpServletRequest request, HttpServletResponse response, VideoCollectionOutput entity) {
        JSONObject json = new JSONObject();
        int number = videoCollectionService.edit(entity);
        if (number == 1) {
            json.put("missage", "success");
        } else {
            json.put("missage", "error");
        }
        return json;
    }

    @ResponseBody
    @RequestMapping("deleteVideoCollection")
    public JSONObject deleteVideoCollection(HttpServletRequest request, HttpServletResponse response, int id) {
        JSONObject json = new JSONObject();
        int number = videoCollectionService.deleteVideoCollection(id);
        if (number == 1) {
            json.put("missage", "success");
        } else {
            json.put("missage", "error");
        }
        return json;
    }


}
